package com.mt.channel.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mt.channel.api.service.IChannelAdminService;
import com.mt.channel.entity.ChannelAdminEntity;
import com.mt.channel.entity.ChannelAdminRoleRelateEntity;
import com.mt.channel.entity.ChannelRoleMenuRelateEntity;
import com.mt.channel.service.model.ChannelAdminRoleRelateServiceModel;
import com.mt.channel.service.model.ChannelAdminServiceModel;
import com.mt.channel.service.model.ChannelRoleMenuRelateServiceModel;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.common.constants.BaseErrorConstants;
import org.springframework.stereotype.Service;

@Service
public class ChannelAdminServiceImpl implements IChannelAdminService {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChannelAdminServiceImpl.class);

    @Autowired ChannelAdminServiceModel channelAdminServiceModel;

    @Autowired ChannelAdminRoleRelateServiceModel channelAdminRoleRelateServiceModel;

    @Autowired
    ChannelRoleMenuRelateServiceModel channelRoleMenuRelateServiceModel;

    public ServiceResult<ChannelAdminEntity> findInfo(Map<String, Object> params) {
        // 查询用户信息
        ServiceResult<ChannelAdminEntity> result = null;
        try {
            result = channelAdminServiceModel.selectInfo(params);
            if (result.getSuccess() && result.getResult() != null) {
                ChannelAdminEntity channelAdminEntity = result.getResult();
                // 查询用户关联角色信息
                Map<String, Object> roleParams = new HashMap<String, Object>();
                roleParams.put("channelAdminId", channelAdminEntity.getChannelAdminId());
                ServiceResult<List<ChannelAdminRoleRelateEntity>> channelAdminRoleRelateResult = channelAdminRoleRelateServiceModel
                        .selectList(roleParams, null);
                if (channelAdminRoleRelateResult.getSuccess() && channelAdminRoleRelateResult.getResult() != null
                        && channelAdminRoleRelateResult.getResult().size() > 0) {
                    List<ChannelAdminRoleRelateEntity> roleRelateResult = channelAdminRoleRelateResult.getResult();
                    Map<String, Object> roleMenuParams = null;
                    for (ChannelAdminRoleRelateEntity channelAdminRoleRelateEntity : roleRelateResult) {
                        // 查询角色权限信息
                        roleMenuParams = new HashMap<String, Object>();
                        roleMenuParams.put("channelRoleId", channelAdminRoleRelateEntity.getChannelRoleId());
                        ServiceResult<List<ChannelRoleMenuRelateEntity>> channelRoleMenuRelateResult = channelRoleMenuRelateServiceModel
                                .selectList(roleMenuParams, null);
                        if (channelRoleMenuRelateResult.getSuccess() && channelRoleMenuRelateResult.getResult() != null
                                && channelRoleMenuRelateResult.getResult().size() > 0) {
                            channelAdminRoleRelateEntity.getChannelRoleEntity()
                                    .setChannelRoleMenuList(channelRoleMenuRelateResult.getResult());
                        }
                    }
                    channelAdminEntity.setChannelAdminRoleList(roleRelateResult);
                }
            }
        } catch (Exception e) {
            LOGGER.error("[ChannelAdminServiceImpl][findInfo]：query findInfo occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询详情错误");
        }
        return result;
    }

    public ServiceResult<List<ChannelAdminEntity>> findList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        ServiceResult<List<ChannelAdminEntity>> result = new ServiceResult<List<ChannelAdminEntity>>();
        try {
            result = channelAdminServiceModel.selectList(params, pagerInfo);
        } catch (Exception e) {
            LOGGER.error("[ChannelAdminServiceImpl][findList]：query findList occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询列表错误");
        }

        return result;
    }

    public ServiceResult<Integer> doInsert(ChannelAdminEntity channelAdminEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = channelAdminServiceModel.insert(channelAdminEntity);
        } catch (Exception e) {
            LOGGER.error("[ChannelAdminServiceImpl][doInsert]：query doInsert occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdateRelate(ChannelAdminEntity channelAdminEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = channelAdminServiceModel.updateRelate(channelAdminEntity);
        } catch (Exception e) {
            LOGGER.error("[ChannelAdminServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "编辑信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doDeleteById(Integer channelAdminId) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = channelAdminServiceModel.deleteByPrimaryKey(channelAdminId);
        } catch (Exception e) {
            LOGGER.error("[ChannelAdminServiceImpl][doDeleteById]：query doDeleteById occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "删除信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdate(ChannelAdminEntity channelAdminEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = channelAdminServiceModel.updateByPrimaryKeySelective(channelAdminEntity);
        } catch (Exception e) {
            LOGGER.error("[ChannelAdminServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "更新信息错误.");
        }
        return result;
    }

}
